import { getStorageItem, setStorageItem, delStorageItem } from '@/utils/storage'
import { AesEncrypt,AesDecode} from './cryptoJsUtil'
import { dataFlag } from '../settings';
const TokenKey = 'racs-token'
const ShareTokenKey = 'shareToken'
const AccessUserKey = 'gaeaUser'

export function getToken() {
  return getStorageItem(TokenKey)
}
export function getShareToken() {
  return getStorageItem(ShareTokenKey) == null ? '' : getStorageItem(ShareTokenKey);
}
export function setToken(token) {
  return setStorageItem(TokenKey, token)
}
export function setShareToken(shareToken) {
  return setStorageItem(ShareTokenKey, shareToken)
}
export function delToken() {
  return delStorageItem(TokenKey)
}
export function delShareToken() {
  return delStorageItem(ShareTokenKey)
}

export function getAccessUser() {
  return getStorageItem(AccessUserKey)
}
export function setAccessUser(accessUser) {
  return setStorageItem(AccessUserKey, accessUser)
}
export function delAccessUser() {
  return delStorageItem(AccessUserKey)
}

// 获取当前用户的所有的权限码，判断是否有
export function hasPermission(permissionStr) {
  // 不需要权限
  if (permissionStr == null || permissionStr.length == 0) {
    return true
  }

  // 登录用户的全部权限码
  var user = getAccessUser()
  if (user == null || user.authorities == null) {
    return false
  }
  var opAuthoritiesStr = JSON.stringify(user.authorities)

  // permissionStr可能是：authorityManage、authorityManage:insert、authorityManage:insert|authorityManage:update
  var needPermissionArray = permissionStr.split('|')
  for (var i = 0; i < needPermissionArray.length; i++) {
    // 只要有其中的一个权限，就返回true
    var needPermission = needPermissionArray[i] // authorityManage、authorityManage:insert
    needPermission = needPermission.replace(/\ /g, "") // 去除authorityManage : insert中:前后的空格

    if(opAuthoritiesStr.indexOf(needPermission)>=0){
      return true
    }
  }
  return false
}

const dataOptAhr = {};
export function setDataAuthority(dataGrp) {
  const tpMap = {
    // MENU0102020141: 'site',
    // MENU0102020341: 'staff',
    // MENU0102020390: 'newstaff',
    // MENU0102020441: 'org',
    // MENU0102020541: 'academy',
    // MENU0102020800: 'folkrlgsiteSTD',
    // MENU0102020941: 'record',

    0: 'org',
    1: 'site',
    2: 'staff',
    3: 'newStaff',
    4: 'academy',
    5: 'folkrlgsite',
    6: 'record',

    7: 'abroad',
    8: 'illegalActpoint',
    9: 'illegalOrgCoreMember',
    10: 'affected',

    // MENU0103000200: 'IllegalSite',
    // MENU0103000400: 'IllegalStaff',
    // MENU0103000600: 'IllegalOrg',
    // MENU0103000800: 'OutsideOrg',
    // MENU0103001000: 'IllegalRlg',
  };
  for (const key in dataOptAhr) {
    delete dataOptAhr[key]
  }
  dataGrp?.forEach(menu => {
    if (tpMap[menu.dataTp]) dataOptAhr[tpMap[menu.dataTp]] = menu.optAhrArr;
  });
}

export function getUserInfo() {
  if(dataFlag === 0) {
    const userInfoEncode = localStorage.getItem("racs-userInfo")
    let userInfoUncode = ''
    if(userInfoEncode === null) {
      return {}
    }else if(userInfoEncode.indexOf('city_cd') > -1) {
      let userInfoEncode1 = AesEncrypt(userInfoEncode)
      userInfoUncode = AesDecode(userInfoEncode1)
    }else{
      userInfoUncode = AesDecode(userInfoEncode)
    }
    if (userInfoUncode) {
      return JSON.parse(userInfoUncode);
    } else {
      return {};
    }
  }else{
    const userInfo = localStorage.getItem("racs-userInfo");
    if (userInfo) {
      try {
        return JSON.parse(userInfo);
      } catch (error) {
      }
    }
    return {};
  }
}
